package com.boost.socialNetwork.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

public class HistoryManager extends History {

	private static String username;
	private static String IdeaName;
	private static boolean canDelete;
	static Login log = null;
	static IdeaPage idea = null;
	static Home home = null;
	static Registration reg = null;
	static IdeaEntry ideaEntry = null;
	static String oldIdea = "";
	static SideBar sideBar=null;

		
	
	public HistoryManager() {

	}

	public static void HistoryChange(ValueChangeEvent<String> event) {
		System.out.println("history changee");
		String chosen = event.getValue();
		final UserServiceAsync userService = GWT
		.create(UserService.class);
		if (chosen.substring(0, 4).equals("Page")) {
			if (chosen.equals("PageLogin")) {

//				userService.putCurrentPage("PageLogin", new AsyncCallback<String>() {					
//					public void onSuccess(String result) {												
//					}									
//					public void onFailure(Throwable caught) {												
//					}
//				});
				if (log == null) {

					log = new Login();
				}
				else{
					log.reset();
				}
					
				home=null;
				idea=null;
				reg=null;

				if (sideBar != null) {
					RootPanel.get("sidebar").clear();
					RootPanel.get("sidebar").remove(sideBar);
				}


				RootPanel.get("head").clear();
				RootPanel.get("post").clear();
				RootPanel.get("head").add(log.getHead());
				RootPanel.get("post").add(log);


			} else if (chosen.equals("PageReg")) {
//				userService.putCurrentPage("PageReg", new AsyncCallback<String>() {					
//					public void onSuccess(String result) {												
//					}									
//					public void onFailure(Throwable caught) {												
//					}
//				});
				
				if (reg == null) {

					reg = new Registration();

				}

				RootPanel.get("post").clear();
				RootPanel.get("head").clear();
				RootPanel.get("post").add(reg);
				RootPanel.get("head").add(reg.getHead());
				RootPanel.get("post").getElement().getStyle()
						.setPosition(Position.RELATIVE);

			} else if (chosen.equals("PageHome")) {
				System.out.println("HOME FIREDDDD");
				
//				userService.putCurrentPage("PageHome", new AsyncCallback<String>() {					
//					public void onSuccess(String result) {												
//					}									
//					public void onFailure(Throwable caught) {												
//					}
//				});
				System.out.println("entered home");
				if (home == null)
					home = new Home();
				else {
					home.getMyIdeas().clear();
					home.getOthers().clear();
					home.getIdeas();
				}

				if (log != null) {
					RootPanel.get("post").clear();

					RootPanel.get("head").clear();

					RootPanel.get("sidebar").clear();
				}

				home.setHead("ya brence");

				RootPanel.get("head").add(home.getHead());

				
				RootPanel.get("post").add(home);

				
				SideBar side = new SideBar();

				RootPanel.get("sidebar").add(side);
				
			} else if (chosen.equals("PageIdea")) {

//				userService.putCurrentPage("PageIdea", new AsyncCallback<String>() {					
//					public void onSuccess(String result) {												
//					}									
//					public void onFailure(Throwable caught) {												
//					}
//				});
				System.out.println("el Ideaa" + IdeaName);
				if (idea == null || !IdeaName.equals(oldIdea))
					idea = new IdeaPage(IdeaName, canDelete);				

				oldIdea = IdeaName;
				RootPanel.get("post").clear();
				RootPanel.get("head").clear();
				RootPanel.get("post").add(idea);
			}

			else if (chosen.equals("PageIdeaEntry")) {
				userService.putCurrentPage("PageIdeaEntry", new AsyncCallback<String>() {					
					public void onSuccess(String result) {												
					}									
					public void onFailure(Throwable caught) {												
					}
				});
				System.out.println("1234");
				if (ideaEntry == null)
					ideaEntry = new IdeaEntry();

				RootPanel.get("post").clear();
				RootPanel.get("head").clear();
				RootPanel.get("post").add(ideaEntry);
				RootPanel.get("head").add(ideaEntry.getHead());

			}

		} else {
			if (log == null)
				log = new Login();
			RootPanel.get("post").clear();
			RootPanel.get("head").clear();
			RootPanel.get("post").add(log);
			RootPanel.get("head").add(log.getHead());
		}

	}

	public static void newHistory(String page) {
		newItem(page);

	}

	public static void setLog(Login log) {
		HistoryManager.log = log;
	}

	public static void setIdea(IdeaPage idea) {
		HistoryManager.idea = idea;
	}

	public static void setHome(Home home) {
		HistoryManager.home = home;
	}

	public static void setReg(Registration reg) {
		HistoryManager.reg = reg;
	}

	public static void setIdea(String idea) {
		IdeaName = idea;
	}

	public static void setCanDelete(boolean canDelete) {
		HistoryManager.canDelete = canDelete;
	}

	public static void setUsername(String username) {
		HistoryManager.username = username;
	}

}
